demand u>=
demand d+
demand read-file
demand file-position
demand reposition-file
supplied-by
13 constant <cr>
10 constant <lf>
: cr|lf? ( char -- flag )  dup <cr> = swap <lf> = or ;
: newline? ( c-addr u -- c-addr u u2 )
	over c@ <lf> = if 1 else
		over c@ <cr> = if
			dup 2 u< if 1 else
				over char+ c@ <lf> = if 2 else 1 then
			then
		else 0 then
	then ;
\ get offsets before and after the first newline in C-ADDR U.
: find-newline ( c-addr u -- u' u2 )
	tuck begin dup  dup while  drop newline? ?dup 0= while
		1 /string
	repeat then >r  nip -  dup r> + ;

: (read-line) ( c-addr u fileid -- u' u2 ior )
	over >r
	>r over swap 2 + r> read-file  ?dup 0= if
		find-newline  over r@ u>= if 2drop r@ r@ then
	0 then r> drop ;
: read-line ( c-addr u fileid -- u' flag ior )
	dup >r
	dup file-position ?dup if >r 2drop drop r> else 2>r
		(read-line) ?dup 0= if
			dup 0<> swap
			0 2r> d+ r@ reposition-file
		else 2r> 2drop then
	then r> drop ;
